package com.itheima;


import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableConfigurationProperties({AliyunOssConfig.class, QiniuyunOssConfig.class})
public class AliQiAutoConfiguration {
    // 自动装配AliyunUploadService和QiniuUploadService
    @Bean
    @ConditionalOnProperty(name = "upload.type", havingValue = "aliyun", matchIfMissing = true)
    public UploadService aliyunUploadService(AliyunOssConfig aliyunOssConfig) {
        return new AliyunUploadService(aliyunOssConfig); // 或注入依赖的Bean
    }

    @Bean
    @ConditionalOnProperty(name = "upload.type", havingValue = "qiniu")
    public UploadService qiniuUploadService(QiniuyunOssConfig qiniuyunOssConfig) {
        return new QiniuUploadService(qiniuyunOssConfig); // 或注入依赖的Bean
    }

    // 创建一个默认的UploadService Bean，用于注入到其他组件中
//    @Bean
//    @Primary // 确保只有一个UploadService Bean被注入
//    public UploadService uploadService(
//            @Qualifier("aliyunUploadService") UploadService aliyunUploadService,
//            @Qualifier("qiniuUploadService") UploadService qiniuUploadService,
//            Environment environment) {
//        String uploadType = environment.getProperty("upload.type", "aliyun");
//        switch (uploadType) {
//            case "aliyun":
//                return aliyunUploadService;
//            case "qiniu":
//                return qiniuUploadService;
//            default:
//                throw new IllegalArgumentException("Unsupported upload.type: " + uploadType);
//        }
//    }
}
